//index.js
//获取应用实例
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');

Page({
  data: {
    markers: [],
    shopList: [], //店铺列表 
    show_authorization: false,  //授权判断
    scale:14
  },
  onLoad: function (options) {
    if (options.lat && options.lng) {
      this.setData({
        searchLatitude: options.lat,
        searchLongitude: options.lng
      })
    }

    if (options.sku_code){
      this.setData({
        sku_code: options.sku_code
      })
    }
  },
  onShow: function () {
    this.setData({
      shopName: wx.getStorageSync('shopList_active').shop_name,
      shopAddress: wx.getStorageSync('shopList_active').address,
      distance: wx.getStorageSync('shopList_active').distance,
      latitude: wx.getStorageSync('shopList_active').add_info.location_latitude,
      longitude: wx.getStorageSync('shopList_active').add_info.location_longitude,
      navigationLatitude: wx.getStorageSync('shopList_active').add_info.location_latitude,
      navigationLongitude: wx.getStorageSync('shopList_active').add_info.location_longitude,
      shopId: wx.getStorageSync('shopList_active').id,
      business_hours: wx.getStorageSync('shopList_active').business_hours,
    })

    if (this.data.searchLatitude && this.data.searchLongitude) {
      var searchMarker = [{
        id: 'search',
        latitude: this.data.searchLatitude,
        longitude: this.data.searchLongitude,
        iconPath: '/images/ic_point_b.png',
        width: 37,
        height: 50,
      }]
      this.setData({
        markers: searchMarker
      })
    }else{
      this.setData({
        markers: []
      })
    }
    this.setData({
      img_url: app.globalData.base_url + 'images/shopList_01.jpg'
    })

    this.adjustLocation();

  },
  //获取店铺列表
  getMarker: function () {
    var that = this;
    var params ={};
    params.latitude = this.data.latitude;
    params.longitude = this.data.longitude;
    params.current_latitude = this.data.current_latitude;
    params.current_longitude = this.data.current_longitude;
    if (this.data.sku_code){
      params.sku_code = this.data.sku_code;
    }
    
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shop/shopList',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var shop = res.data.data.currentList;
          var shopId = that.data.shopId;
          if (shop.length > 0) {
            var markers = that.data.markers, shopList = [];
            for (let i in shop) {
              if (shop[i].id == shopId){
                var marker = {
                  id: i,
                  latitude: shop[i].add_info.location_latitude,
                  longitude: shop[i].add_info.location_longitude,
                  iconPath: '/images/ic_point_a.png',
                  width: 60,
                  height: 60,
                }
              }else{
                var marker = {
                  id: i,
                  latitude: shop[i].add_info.location_latitude,
                  longitude: shop[i].add_info.location_longitude,
                  iconPath: '/images/ic_point.png',
                  width: 50,
                  height: 50,
                }
              }
              
              markers.push(marker)
              var new_shop = shop[i];
              new_shop.shopImage = ((new_shop.shopImage == '' || new_shop.shopImage == undefined) ? '/images/icon_59.png' : new_shop.shopImage);
              shopList.push(new_shop)
            }
          }
          that.setData({
            shopList: shop,
            markers: markers
          })
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网络错误");
      }
    })
  },
  //定位
  adjustLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          current_latitude: res.latitude,
          current_longitude: res.longitude,
          show_authorization: false
        })
        that.getMarker();
      },
      fail: function (fail_res) {
        if (fail_res.errMsg == "getLocation:fail auth deny" || fail_res.errMsg == "getLocation:fail:auth denied") {
          that.setData({
            show_authorization: true
          })
        } else {
          util.showError("定位失败");
        }
      }
    })
  },
  //点击标记点触发
  markerTap: function (e) {
    var that = this;
    var markerId = e.markerId;
    if (markerId =='search'){
      return false
    }
    var shop = that.data.shopList[markerId]
    wx.setStorageSync('shopList_active', shop)
    var markers = that.data.markers;
    for (var i = 0; i < markers.length; i++) {
      if (markers[i].id == markerId) {
        markers[i].iconPath = '/images/ic_point_a.png',
          markers[i].width = 60
        markers[i].height = 60
      } else if (markers[i].id != "search"){
        markers[i].iconPath = '/images/ic_point.png',
          markers[i].width = 50
        markers[i].height = 50
      }
    }
    that.setData({
      markers: markers,
      shopName: shop.shop_name,
      shopAddress: shop.address,
      distance: shop.distance,
      navigationLatitude: shop.add_info.location_latitude,
      navigationLongitude: shop.add_info.location_longitude,
      shopId: shop.id
    })
  },

  //获取授权信息
  getSetting: function () {
    var that = this;
    setTimeout(function () {
      that.adjustLocation();
    }, 300)
  },
  onHide: function () {
    this.setData({
      show_authorization: false
    })
  },
  // 导航
  navigation: function () {
    var latitude = this.data.navigationLatitude / 1;
    var longitude = this.data.navigationLongitude / 1;
    // console.log(latitude);
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  },
  //查看店内商品
  shopProduct: function () {
    var shopId = this.data.shopId
    wx.navigateTo({
      url: '/map/pages/shopProduct/shopProduct?shopId=' + shopId+'&index=1'
    })
  }

})
